class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


def addTwoNumbers(l1, l2):
    # 创建头节点
    head = ListNode(0)
    # 当前节点指针，构造结果链表
    current = head
    # 进位标志，初始为0
    carry = 0

    while l1 or l2 or carry:
        val1 = l1.val if l1 else 0

        val2 = l2.val if l2 else 0

        carry, out = divmod(val1 + val2 + carry, 10)

        current.next = ListNode(out)

        current = current.next

        if l1:
            l1 = l1.next

        if l2:
            l2 = l2.next

    return head.next


l1 = ListNode(2)
l1.next = ListNode(4)
l1.next.next = ListNode(3)

# 创建链表 l2: 5 -> 6 -> 4
l2 = ListNode(5)
l2.next = ListNode(6)
l2.next.next = ListNode(4)

# 计算结果
result = addTwoNumbers(l1, l2)

# 打印结果
while result:
    print(result.val, end=" -> ")
    result = result.next